package com.spring.dao.Impl;

import com.spring.dao.AccountDao;
import com.spring.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;




@Repository  //(value = "accountDaoImpl" )
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private QueryRunner queryRunner;


    @Override
    public List<Account> findAll() {
        String sql = "select * from account";
        List<Account> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public List<Account> findById(Integer id) {
        String sql = "select * from account where id = ?";
        List<Account> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<Account>(Account.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void Save(Account account) {
       String sql = "insert into account values(null,?,?)";
        try {
            queryRunner.update(sql,account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void Update(Account account) {
        String sql = "update account set name=? , money=? where id = ?";
        try {
            queryRunner.update(sql,account.getName(),account.getMoney(),account.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void Delete(Integer id) {
       String sql = "delete from account where id = ?";
        try {
            queryRunner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
